package com.dexilaonian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.Okhttp;
import com.dexilaonian.utils.PublicUtil;
import com.dexilaonian.utils.WorksSizeCheckUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class Phone_Find_PwdActivity extends BaseActivity {


    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title_top)
    TextView titleTop;
    @Bind(R.id.base_login)
    TextView baseLogin;
    @Bind(R.id.et_find_phone)
    EditText etFindPhone;
    @Bind(R.id.btn_find_pwd)
    Button btnFindPwd;
    private String myphone;
    private String find_url= Constant.base_url + "/verify/sentSmsVerify";

    @Override
    protected void initData() {
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_phone__find__pwd;
    }

    @Override
    public void initView() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        titleTop.setText("忘记密码");
        WorksSizeCheckUtil.textChangeListener textChangeListener = new WorksSizeCheckUtil.textChangeListener(btnFindPwd);
        textChangeListener.addAllEditText(etFindPhone);
        WorksSizeCheckUtil.setChangeListener(new WorksSizeCheckUtil.IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if (isHasContent) {
                    btnFindPwd.setBackground(getResources().getDrawable(R.drawable.button));
                    btnFindPwd.setClickable(true);
                } else {
                    btnFindPwd.setBackground(getResources().getDrawable(R.drawable.graybutton));
                    btnFindPwd.setClickable(false);
                }
            }
        });

    }

    @OnClick({R.id.back, R.id.base_login, R.id.btn_find_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.base_login:
                startActivity(new Intent(Phone_Find_PwdActivity.this, LoginActivity.class));
                finish();
                break;
            case R.id.btn_find_pwd:
                myphone = etFindPhone.getText().toString().trim();
                if (TextUtils.isEmpty(myphone) || myphone.length() != 11 || !PublicUtil.isMobileNO(myphone)) {
                    showToast("手机号码不正确");
                } else {
                    sendPhone(myphone);
                }
                break;
        }
    }

    private void sendPhone(final String myphone2) {
        showProgressBar(true);
        OkHttpUtils.post().url(find_url).addParams("phone",myphone2).addParams("type","1").build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
                showProgressBar(false);
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("xuxux", response);
                showProgressBar(false);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int data = Integer.parseInt(jsonObject.optString("status"));
                    Log.e("data", response);
                    if (data == 7) {
                        showToast("单手机号短信发送时间间隔少于1分钟");
                        return;
                    }
                    if (data == 6) {
                        showToast("单手机号单日发送短信超10条");
                        return;
                    }
                    if (data == 0) {
                        showToast("发送成功，请注意查收短信");
                        Intent intent = new Intent(Phone_Find_PwdActivity.this, Register_smsActivity.class);
                        intent.putExtra("str_phone_find", myphone2);
                        startActivity(intent);
                    } else {
                        showToast("发送失败");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
